package physics.MovementControl;

import physics.GameObject;

import common.Math3D;
import common.Point3D;


public class SatelliteMovementController extends AbstractMovementController {

	double height = 1;
	double min_radius_height = 0;
	
	public SatelliteMovementController(GameObject object) {
		super(object);
		hasMomentum = true;
		movementFriction = 0.002;
		movementLimit = 0.1;
		rotationFriction = 0.004;
	}
	
	

	@Override
	public void play() {
		fixSatellitePosition();
		super.play();
	}



	@Override
	protected void doMoveMomentum() {

		object.getCenter().sub(object.getPlanet().getCenter());
		object.getCenter().normalize(height);

		double alpha = movementMomentum.norm() / height;
		double distance_fwd = height * Math.sin(alpha);
		double distance_cnt = height * (Math.cos(alpha) -1 );
		
		Point3D cnt = object.getCenter().clone(); 
		cnt.normalize(distance_cnt);
		Point3D fwd = movementMomentum.clone();
		fwd.normalize(distance_fwd);
				
		object.getCenter().add(fwd);
		object.getCenter().add(cnt);
		
		
		Math3D.fixToOrthogonal(movementMomentum,cnt	);
		object.getCenter().normalize(height);
		object.getCenter().add(object.getPlanet().getCenter());
		object.getPlanet().calculateFacingVectors(object);
	}
	
	private void fixSatellitePosition(){
		object.getCenter().sub(object.getPlanet().getCenter());
		height = object.getCenter().norm();
		
		
		
		height = Math.max(height,object.getPlanet().getRadius() +  Math.max(0,object.getPlanet().getHeightFactor()) + min_radius_height);
		object.getCenter().normalize(height);
		object.getCenter().add(object.getPlanet().getCenter());
		object.getPlanet().calculateFacingVectors(object);
	}

	@Override
	protected void doRotationMomentum() {
		super.doRotationMomentum();
	}
	
	
	
}
